﻿var hasOpenWindow = false;
function showopen(url, width, height, name) {
    if (!(name)) {
        name = '';
    }
    //如果不想打开很多的弹出窗口，可以把name属性统一。可以做到只弹出一个窗口，显示所有的URL
    name = name.replace(/[^\w]/g, '');//name参数很复杂，建议填''
    var iTop = (window.screen.availHeight - 30 - height) / 2;
    var iLeft = (window.screen.availWidth - 10 - width) / 2;
    openwindow = window.open(url, name, "z-look=1003,scrollbars=yes,top=" + iTop + ",left=" + iLeft + ",height=" + height + ",width=" + width);
    openwindow.focus();
    return openwindow;
}
function showmodal(url,width, height, callBack) {
    if (hasOpenWindow == false) {
        hasOpenWindow = true;
        window.AspfmcallBack = callBack;
        showdiv();
        if (url.indexOf("?") == -1) {
            url = url + '?1=1';
        }
        window.myNewWindow = showopen(url + '&showmodal', width, height);
        var loop = setInterval(function () {
            if (window.myNewWindow.closed) {
                clearInterval(loop);
                hasOpenWindow = false;
                hidediv();
            }
        }, 1000);
        window.myNewWindow.focus();
    }
    else {
        showopenerwindow();
    }
}
function showopenerwindow()
{
    if (window.myNewWindow) {
        window.myNewWindow.focus();
    }
}
function closeAspfmPage(returnValue, close) {
    if (window.opener != undefined && window.opener.returnAspfmPage) {
        window.opener.returnAspfmPage(returnValue);
    }
    else {
        window.returnValue = returnValue;
    }
    if (close) {
        if (!!(window.opener)) {
            if (location.search.substr(location.search.length - 10, 10) == "&showmodal") {
                window.close();
            }
        }
    }
    return window.closed;
}
function returnAspfmPage(option) {
    if (hasOpenWindow == true && window.AspfmcallBack)
    {
        window.AspfmcallBack(option);
    }
}
function GetParentAspfmParams() {
    if (window.opener != undefined && window.opener.Aspfm) {
        return window.opener.Aspfm;
    }
    return null;
}
var TimeOnMsg;
function OnMsg() {
    if (TimeOnMsg) window.clearTimeout(TimeOnMsg);
    CheckLoginTime();
    $.ajax({
        type: "post",
        url: _path + "AspfmApi/Login/OnMsg6666677778888",
        success: function (data, status) {
            if (data == "") {
                alert('登录信息过期，请重新登录');
                window.location.href = _path + "login.aspx";
            }
            else if (data == "0") {
                alert('用户在另一处登录，被强制退出。');
                window.location.href = _path + "login.aspx";
            }
            else {
                TimeOnMsg = setTimeout("OnMsg()", 200000);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert('error:' + XMLHttpRequest.status + "  errmsg:" + XMLHttpRequest.responseText);
        }
    });
}
//每秒去更新iframe高度。太消耗CPU,最多在主界面调用一次。
var TimeIframeCounter;
function SetIframeHeight(timeout) {
    if (timeout != true) {
        if (TimeIframeCounter) {
            return;
        }
    }
    if (TimeIframeCounter) window.clearTimeout(TimeIframeCounter);
    var iframelist = $('iframe[Aspfm]');
    if (iframelist.length > 0) {
        iframelist.each(function () {
            if (this.contentWindow.getchildheight) {
                var height = parseInt(this.contentWindow.getchildheight()) + 18;
                var height1 = $(this).height();
                if (Math.abs(height1 - height) > 10) {
                    if (height > 600)//最小高度
                    {
                        $(this).height(height);
                        SendSizeToParent();
                    }
                    else {
                        $(this).height(600);
                        SendSizeToParent();
                    }
                }
            }
        });
        TimeIframeCounter = setTimeout("SetIframeHeight(true)", 2000);
    }
}
function getchildheight() {
    return document.getElementsByTagName('body')[0].offsetHeight;
}
function SendSizeToParent() {
    if (window.parent != window) {
        window.parent.SetIframeHeight();
    }
}
$(function () {
    SetIframeHeight();
    SendSizeToParent();
});